package org.richin.io.fastStream.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

public class RandomAccessFileInputStream extends InputStream
{
  RandomAccessFile raf;

  public RandomAccessFileInputStream(String path)
    throws IOException
  {
    this(new File(path));
  }

  public RandomAccessFileInputStream(File file) throws IOException {
    this(new RandomAccessFile(file, "rw"));
  }

  public RandomAccessFileInputStream(RandomAccessFile raf) {
    this.raf = raf;
  }

  public int read() throws IOException {
    return this.raf.read();
  }

  public int read(byte[] b) throws IOException {
    int n = this.raf.read(b, 0, b.length);
    return (((n <= 0) && (b.length > 0)) ? -1 : n);
  }

  public int read(byte[] b, int off, int len) throws IOException {
    int n = this.raf.read(b, off, len);
    return (((n <= 0) && (len > 0)) ? -1 : n);
  }

  public void write(int b) throws IOException {
    this.raf.write(b);
  }

  public void write(byte[] b) throws IOException {
    this.raf.write(b, 0, b.length);
  }

  public void write(byte[] b, int off, int len) throws IOException {
    this.raf.write(b, off, len);
  }

  public long skip(long n) throws IOException {
    return this.raf.skipBytes((int)n);
  }

  public int available() throws IOException {
    return (int)(this.raf.length() - this.raf.getFilePointer());
  }

  public void seek(long pos) throws IOException {
    this.raf.seek(pos);
  }

  public long getFilePointer() throws IOException {
    return this.raf.getFilePointer();
  }

  public long length() throws IOException {
    return this.raf.length();
  }

  public void close() throws IOException {
    this.raf.close();
  }
}